<template>
	<view>
		<view>
			<button type="primary" :loading="loading" :disabled="disabled" @click="upload" :class="{ active : active }"
				:style="{'height':height,'line-height':lineheight}">
				<image class="headimg-img" v-if="src" :src="src" :style="{'height':height,'width':width}"></image>
				<image class="headimg-img" v-else :src="headimg" :style="{'height':height,'width':width}"></image>
			</button>
		</view>
	</view>

</template>
<script>
	var _self;
	export default {
		data() {
			return {
				isShow: false,
				percent: 0,
				loading: false,
				disabled: false,
				headimg: '/static/images/touxiang.png',
				active: false,
			}
		},
		props: ['src', 'type', 'width', 'height', 'lineheight'],

		methods: {
			upload: function() {
				this.isShow = true;
				this.active = true;
				let token = uni.getStorageSync('token');

				_self = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						const tempFilePaths = res.tempFilePaths;
						const uploadTask = uni.uploadFile({
							url: 'https://wine.hnliandesy.com/api/upload',
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {
								'token': token
							},

							success: (uploadFileRes) => {
								if (uploadFileRes.data) {
									uni.$emit('changeCover', uploadFileRes.data);
								}
							}
						});

						uploadTask.onProgressUpdate(function(res) {
							_self.percent = res.progress;
							// this.$api.msg('上传进度为 '.this.percent)
						});
					},
					error: function(e) {
						console.log(e);
					}
				});
			},

		},
		onLoad: function() {

		}
	}
</script>

<style scoped>
	uni-button[type=primary] {
		color: #fff;
		background-color: #fff;
		height: 170rpx;
	}

	.headimg-img {
		margin-left: -27rpx;

	}
</style>
